﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Kassasysteem_TacoTruijens
{
    public partial class voorstellingen : System.Web.UI.Page
    {
        private DataClassesDataContext Database = new DataClassesDataContext();

        protected void Page_Load(object sender, EventArgs e)
        {
            addHeaderRowToTableShows();
            fillTableShows();
        }

        private void fillTableShows()
        {
            var shows = (from s in Database.Voorstellings
                        where s.datumtijd >= DateTime.Now
                        orderby s.datumtijd ascending
                        select s).Take(30);

            String movieName = Request.QueryString["moviename"];

            if (movieName != null)
            {
                shows = null;
                shows = from s in Database.Voorstellings
                        where s.datumtijd >= DateTime.Now
                        where s.film_naam.Contains(movieName)
                        orderby s.datumtijd ascending
                        select s;

                labelSearch.Text = "U heeft gezocht op filmnaam '" + movieName + "'.";
            }

            Boolean even = false;

            foreach (Voorstelling s in shows)
            {
                TableRow showTableRow = new TableRow();

                if (even)
                {
                    showTableRow.CssClass = "even";
                    even = false;
                }
                else
                {
                    even = true;
                }

                TableCell dateTimeTableCell = new TableCell();
                TableCell movieNameTableCell = new TableCell();
                TableCell freeSpacesTableCell = new TableCell();
                TableCell buyTableCell = new TableCell();

                buyTableCell.CssClass = "alignCenter";
                
                dateTimeTableCell.Text = s.datumtijd.ToString();
                movieNameTableCell.Text = s.Film.naam;
                freeSpacesTableCell.Text = getFreeSpaces(s).ToString();
                buyTableCell.Text = "<a href='choosetickets.aspx?datetime=" + s.datumtijd.ToString() + "&roomnumber=" + s.zaal_nummer.ToString() + "&moviename=" + s.Film.naam.Replace((char)39, ' ') + "'><img src='images/buy.png' alt='Koop' title='Koop tickets' /></a>";

                showTableRow.Cells.Add(dateTimeTableCell);
                showTableRow.Cells.Add(movieNameTableCell);
                showTableRow.Cells.Add(freeSpacesTableCell);
                showTableRow.Cells.Add(buyTableCell);

                tableShows.Rows.Add(showTableRow);
            }
        }

        private int getFreeSpaces(Voorstelling s)
        {
            int returnvalue = 0;

            var chairs = from c in Database.Stoels
                         where c.zaal_nummer == s.zaal_nummer
                         select c;

            var tickets = from t in Database.Tickets
                          where t.Voorstelling == s && (t.Reservering == null || (t.Voorstelling.datumtijd >= DateTime.Now.AddMinutes(30)))
                          select t;

            returnvalue = chairs.Count() - tickets.Count();

            return returnvalue;
        }

        private void addHeaderRowToTableShows()
        {
            TableRow headerTableRow = new TableRow();
            headerTableRow.CssClass = "header";

            TableCell dateTimeTableCell = new TableCell();
            TableCell movieNameTableCell = new TableCell();
            TableCell freeSpacesTableCell = new TableCell();
            TableCell buyTableCell = new TableCell();

            dateTimeTableCell.Text = "Datum/Tijd";
            movieNameTableCell.Text = "Filmnaam";
            freeSpacesTableCell.Text = "Vrije plaatsen";
            buyTableCell.Text = "Koop";

            headerTableRow.Cells.Add(dateTimeTableCell);
            headerTableRow.Cells.Add(movieNameTableCell);
            headerTableRow.Cells.Add(freeSpacesTableCell);
            headerTableRow.Cells.Add(buyTableCell);

            tableShows.Rows.Add(headerTableRow);
        }

        protected void buttonSearch_Click(object sender, EventArgs e)
        {
            String movieName = textBoxMovieName.Text;

            if (movieName.Length > 0)
            {
                String queryString = "shows.aspx?moviename=" + movieName;
                Server.Transfer(queryString);
            }
        }
    }
}